package com.example.peibei.ui.fragment;

import android.app.AlertDialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.dingtao.common.bean.Order;
import com.dingtao.common.core.DataCall;
import com.dingtao.common.core.WDFragment;
import com.dingtao.common.core.exception.ApiException;
import com.dingtao.common.util.SPUtils;
import com.dingtao.common.util.UIUtils;
import com.example.peibei.R;
import com.example.peibei.base.SpConstant;
import com.example.peibei.service.presenter.DelateBillPresenter;
import com.example.peibei.service.presenter.HideOrderPresenter;
import com.example.peibei.service.presenter.ISendBillPresenter;
import com.example.peibei.service.presenter.MyCollectOrderPresenter;
import com.example.peibei.ui.activity.EditOrderActivity;
import com.example.peibei.ui.activity.OrderDetailActivity;
import com.example.peibei.ui.adapter.HomeOrderListAdapter;
import com.example.peibei.ui.adapter.MyBillListAdapter;
import com.example.peibei.ui.adapter.MyCollectBillListAdapter;
import com.example.peibei.ui.weight.CenterDialog;
import com.example.peibei.ui.weight.EmptyRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class MyCollectBillFragment extends WDFragment {
    @BindView(R.id.order_list)
    EmptyRecyclerView order_list;
    @BindView(R.id.empty_view)
    LinearLayout empty_view;

    List<Order.RecordsDTO> orders;
    private MyCollectBillListAdapter homeOrderListAdapter;
    private ISendBillPresenter iSendBillPresenter;
    private SPUtils spUtils;
    private String token;
    private DelateBillPresenter delateBillPresenter;
    private HideOrderPresenter hideOrderPresenter;
    private MyCollectOrderPresenter myCollectOrderPresenter;

    @Override
    public String getPageName() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_my_send_bill_normal;
    }

    @Override
    protected void initView() {
        spUtils = new SPUtils(getActivity(), SpConstant.USER_TOKEN);
        token = spUtils.getString(SpConstant.USER_TOKEN);
        orders = new ArrayList<>();
        order_list.setLayoutManager(new LinearLayoutManager(getActivity()));
        homeOrderListAdapter = new MyCollectBillListAdapter(orders);
        order_list.setAdapter(homeOrderListAdapter);
        order_list.setEmptyView(empty_view);
        myCollectOrderPresenter = new MyCollectOrderPresenter(new OrderCall());
        homeOrderListAdapter.setOnItemClickListener((adapter, view, position) -> {
            Order.RecordsDTO order = (Order.RecordsDTO) adapter.getData().get(position);
            String publishOrderId = order.getPublishOrderId();
            Bundle bundle = new Bundle();
            bundle.putString("publishOrderId", publishOrderId);
            intent(OrderDetailActivity.class, bundle);
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        myCollectOrderPresenter.reqeust(true, "Bearer " + token);
    }

    class OrderCall implements DataCall<Order> {
        @Override
        public void success(Order data, Object... args) {
            //添加列表并刷新
            if (myCollectOrderPresenter.getPage() == 1) {
                orders.clear();
            }
            homeOrderListAdapter.setNewInstance(data.getRecords());
            homeOrderListAdapter.notifyDataSetChanged();
        }

        @Override
        public void fail(ApiException e, Object... args) {

        }
    }
}
